//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.timanetworks.comu.utils;

import cn.hutool.core.util.ObjectUtil;
import com.timanetworks.comu.bizexception.BizException;
import com.timanetworks.comu.enums.BizEnum;
import com.timanetworks.comu.enums.BizErrorEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Pattern;

/**
 * 断言类
 */
public class JavaAssert {
    private static final Logger log = LoggerFactory.getLogger(JavaAssert.class);

    public JavaAssert() {
    }
    public static <E extends BizException> void errorThrow(String message) {
        if (true) {
            log.warn("{}", message);
            throwException(BizErrorEnum.PARAM_ERROR, message, null);
        }
    }
    public static <E extends BizException> void errorThrow( BizEnum baseEnum,String message) {
        if (true) {
            log.warn("{}", message);
            throwException(baseEnum, message, null);
        }
    }
    public static <E extends BizException> void isTrue(boolean expression, BizEnum returnCode, String message, Object arg, Class<E> exceptionCls) {
        if (!expression) {
            log.warn("expression is false,{},args:{}", message, arg);
            throwException(returnCode, message, exceptionCls);
        }

    }
    public static <E extends BizException> void isTrue(boolean expression, BizEnum returnCode, String message, Object arg) {
        if (expression) {
            log.warn("expression is false,{},args:{}", message, arg);
            throwException(returnCode, message, null);
        }

    }

    public static <E extends BizException> void isTrue(boolean expression, BizEnum returnCode, String message, Class<E> exceptionCls) {
        if (expression) {
            log.warn("{}", message);
            throwException(returnCode, message, exceptionCls);
        }

    }
    public static <E extends BizException> void isTrue(boolean expression, BizEnum returnCode, String message) {
        if (expression) {
            log.warn("{}", message);
            throwException(returnCode, message, null);
        }
    }
    public static <E extends BizException> void isTrue(boolean expression, String message) {
        if (expression) {
            log.warn("{}", message);
            throwException(BizErrorEnum.PARAM_IS_NULL, message, null);
        }
    }
    public static <E extends BizException> void isEmpty(Object text, BizEnum returnCode, String message, Class<E> exceptionCls) {
        if (ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, message, exceptionCls);
        }

    }
    public static <E extends BizException> void isEmpty(Object text, BizEnum returnCode, String message) {
        if (ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, message, null);
        }

    }
    public static <E extends BizException> void isEmpty(Object text, String message) {
        if (ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(BizErrorEnum.PARAM_IS_NULL, message, null);
        }

    }
    public static <E extends BizException> void notEmpty(Object text, String message) {
        if (! ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(BizErrorEnum.PARAM_IS_NULL, message, null);
        }

    }
    public static <E extends BizException> void notEmpty(Object text, BizEnum returnCode, String message, Class<E> exceptionCls) {
        if (! ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, message, exceptionCls);
        }

    }
    public static <E extends BizException> void notEmpty(Object text, BizEnum returnCode, String message) {
        if (! ObjectUtil.isEmpty(text)) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, message, null);
        }

    }
    public static <E extends BizException> void isNum(String text, BizEnum returnCode, String message, Class<E> exceptionCls) {
        boolean isNum = Pattern.compile("^[0-9]*$").matcher(text).find();
        if (!isNum) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, "不是正整数", exceptionCls);
        }

    }
    public static <E extends BizException> void isNum(String text, BizEnum returnCode, String message) {
        boolean isNum = Pattern.compile("^[0-9]*$").matcher(text).find();
        if (!isNum) {
            log.warn("{},args:{}", message, text);
            throwException(returnCode, "不是正整数", null);
        }

    }

    private static <E extends BizException> void throwException(BizEnum returnCode, String message, Class<E> exceptionCls) {
        if (null == exceptionCls) {
            throw new BizException(returnCode, message);
        } else {
            BizException cls = null;

            try {
                cls = (BizException)exceptionCls.newInstance();
            } catch (InstantiationException var5) {
                log.warn("构建异常失败,Class:{}", exceptionCls, var5.getMessage());
            } catch (IllegalAccessException var6) {
                log.warn("构建异常失败,Class:{}", exceptionCls, var6.getMessage());
            }
//            cls.setErrorMessage(returnCode.getName());
            cls.setErrorMessage(message);
            throw cls;
        }
    }

    public static void main(String[] args) {
        JavaAssert.isTrue(true,"2222");
    }
}
